package com.ruoyi.web.controller.task;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.constant.TemplateConstants;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.task.domain.*;
import com.ruoyi.task.service.IChannelConfigService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.task.service.IEquipmentConfigService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 网点摄像头配置Controller
 * 
 * @author ruoyi
 * @date 2025-04-22
 */
@RestController
@RequestMapping("/equipment/config")
public class EquipmentConfigController extends BaseController
{
    @Autowired
    private IEquipmentConfigService equipmentConfigService;


    /**
     * 查询网点摄像头配置列表
     */
    @PreAuthorize("@ss.hasPermi('equipment:config:list')")
    @GetMapping("/list")
    public TableDataInfo list(EquipmentChannelConfig equipmentConfig)
    {
        startPage();
        List<EquipmentChannelConfig> list = equipmentConfigService.selectEquipmentChannelConfigList(equipmentConfig);
        return getDataTable(list);
    }

    /**
     * 导出网点摄像头配置列表
     */
    @PreAuthorize("@ss.hasPermi('equipment:config:export')")
    @Log(title = "网点摄像头配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EquipmentChannelConfig equipmentConfig) {
        List<EquipmentChannelConfig> list = equipmentConfigService.selectEquipmentChannelConfigList(
                equipmentConfig);
        ExcelUtil<EquipmentChannelConfig> util = new ExcelUtil<EquipmentChannelConfig>(
                EquipmentChannelConfig.class);
        util.exportExcel(response, list, "网点摄像头配置数据");
    }

    /**
     * 获取网点摄像头配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('equipment:config:query')")
    @GetMapping(value = "/{equipmentId}")
    public AjaxResult getInfo(@PathVariable("equipmentId") Long equipmentId)
    {
        return success(equipmentConfigService.selectChannelConfigByChannelId(equipmentId));
    }

    /**
     * 新增网点摄像头配置
     */
    @PreAuthorize("@ss.hasPermi('equipment:config:add')")
    @Log(title = "网点摄像头配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EquipmentConfig equipmentConfig)
    {
        equipmentConfig.setCreateBy(getUsername());
        equipmentConfig.setUpdateBy(getUsername());
        return toAjax(equipmentConfigService.insertEquipmentConfig(equipmentConfig));
    }

    /**
     * 修改网点摄像头配置
     */
    @PreAuthorize("@ss.hasPermi('equipment:config:edit')")
    @Log(title = "网点摄像头配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EquipmentConfig equipmentConfig)
    {
        equipmentConfig.setUpdateBy(getUsername());
        return toAjax(equipmentConfigService.updateEquipmentConfig(equipmentConfig));
    }

    /**
     * 删除网点摄像头配置
     */
    @PreAuthorize("@ss.hasPermi('equipment:config:remove')")
    @Log(title = "网点摄像头配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{equipmentIds}")
    public AjaxResult remove(@PathVariable Long[] equipmentIds)
    {
        return toAjax(equipmentConfigService.deleteEquipmentConfigByEquipmentIds(equipmentIds));
    }

    /**
     * excel批量导入
     */
    @PreAuthorize("@ss.hasPermi('task:config:import')")
    @Log(title = "批量导入", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult importExcel(@RequestParam("file") MultipartFile file) throws IOException {
        ExcelUtil<EquipmentCameraImportConfig> excelUtil = new ExcelUtil<EquipmentCameraImportConfig>(EquipmentCameraImportConfig.class);
        List<EquipmentCameraImportConfig> equipmentCameraImportConfigs = excelUtil.importExcel(file.getInputStream());
        String username = getUsername();
        return success(equipmentConfigService.importEquipmentCameraImportConfig(equipmentCameraImportConfigs, username));
    }


    /**
     * 批量导入模板下载
     * @param response
     * @param
     */
    @Log(title = "模板导出", businessType = BusinessType.EXPORT)
    @PostMapping("/downloadTemplate")
    public void downloadTemplate(HttpServletResponse response) throws IOException {
        try {
            FileUtils.downloadFile(response, "总行录像机摄像头参数配置.xlsx", TemplateConstants.EQUIPMENT_TEMPLATE_PATH);
        } catch (Exception e) {
            logger.error("downloadTemplate error ", e);
            throw new RuntimeException("文件导出失败");
        }
    }
}
